﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TextTemplating;
using System.IO;
using System.CodeDom.Compiler;

namespace ABP.AutoCode
{
   public  class TemplateHelper
    {
        public string TemplateFilePath { get; set; } = @"c:\users\administrator\source\repos\ABP.AutoCode\ABP.AutoCode\bin\Debug\Template\Domain\Entity\Entity.tt";
        public string OutPutFilePath { get; set; } = @"c:\users\administrator\source\repos\ABP.AutoCode\ABP.AutoCode\bin\Debug\Template\Domain\Entity\";


        public void RunCode()
        {

            TextTemplatingEngineHost host = new TextTemplatingEngineHost();
            Engine engine = new Engine();
            host.Session = new TextTemplatingSession();
          
            host.TemplateFileValue = TemplateFilePath;
            var inputText = File.ReadAllText(TemplateFilePath);
            var Output = engine.ProcessTemplate(inputText, host);//按模板生成内容
            string OutputFileName = Path.GetFileNameWithoutExtension(TemplateFilePath);//输出文件名称
            OutputFileName = Path.Combine(OutPutFilePath, OutputFileName)+host.FileExtension;//拼接输出文件路径
            File.WriteAllText(OutputFileName, Output,host.FileEncoding);//写入文件
            foreach (CompilerError error in host.Errors)
            {
                Console.WriteLine(error.ToString());
            }

        }
    }
}
